/**************************************************************************/
/*  Copyright (C) School of Software Engineering of USTC, 2013             
/*                                                                        
/*  FILE NAME: MyMap.h                                                  
/*  PRINCIPAL AUTHOR: SA12226340(Song Zeyu)                                                                                                          
/*  LANGUAGE: C/C++                                                          
/*  TARGET ENVIRONMENT: Linux                                             
/*  DATE OF FIRST RELEASE: 2013-05-13                                    
/*  DESCRIPTION:extract word segmentation to  every spam, the results stored 
/*              in the map ,map format:<string,int>                      
/**************************************************************************/

#ifndef _MAP_H_
#define _MAP_H_
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

map<string,int> mapword[10];                      //initialization ten mapword for every file in folder
map<string,int> spam_map;                         //store the spam mail words
typedef pair<string,int> pair_map;
vector<pair_map> tVector; 

/*
*http://www.yitsoft.com/chap_study/ch_00029/ch_00029.asp
*/

/*****************************************************************
Function Name: InsertMap
Description: implement word inserts in the mapword[index]
Parameter:  
@str: a pointer to string means the word needs to insert  
@index: a Parameter to the str should insert in the mapword[index]

Author: SA12226340(Song Zeyu)
Date: 2013-05-13
******************************************************************/
int InsertMap(char *str,int index)
{
	pair<map<string,int>::iterator,bool>Insert_pair;
	Insert_pair = mapword[index].insert(pair_map(str,0));//first insert ,the count needs not statistics

	if (Insert_pair.second == true)
	{
		return 1;
	}	
	else
	{
		return 0;
	}
}

/*****************************************************************
Function Name: FindMapWord
Description: implement find word in the mapword[index],if exist ,do nothing,
             not exist,insert the mapword[index]
Parameter:  
@str: a pointer to string means the word needs to find 
@index: a Parameter to the str should find in the mapword[index]

Author: SA12226340(Song Zeyu)
Date: 2013-05-13
******************************************************************/
void FindMapWord(char *str,int index)
{
	map<string,int>::iterator iter;
	iter = mapword[index].find(str);
	if (iter != mapword[index].end())   //if the word exists in mapword ,then do nothing
	{
		;
	} 
	else        //if the word does not exist in the mapword,then insert the mapword[index]
	{
		if (InsertMap(str,index) == 1)
		{
			return ;
		} 
		else
		{
			exit(-1);
		}	
	}
}

/*****************************************************************
Function Name: MapWordSize
Description: length of the index mapword
Parameter:  
@index: a Parameter to statistics word numbers in the mapword[index]

Author: SA12226340(Song Zeyu)
Date: 2013-05-13
******************************************************************/
void MapWordSize(int index)
{
	cout<<"mapsize="<<mapword[index].size()<<endl;
}


/*
*sort for the index mapword
*/
int cmp(const pair<string,int>& x,const pair<string,int>& y)
{
	return x.second < y.second;
}

/*****************************************************************
Function Name: sortMapByValue
Description: sort for the mapword[index],store into thetVector
Parameter:  
@index: a Parameter to sort for the mapword[index]
@mapword:a pointer to the mapword[]
@tVector:a container to store the words after sort
Author: SA12226340(Song Zeyu)
Date: 2013-05-13
******************************************************************/
void sortMapByValue(map<string,int> mapword[],vector< pair<string,int> >& tVector,int index)
{
	for(map<string,int>::iterator iter=mapword[index].begin();iter!=mapword[index].end();iter++)
	{
		tVector.push_back(make_pair(iter->first,iter->second));
	}
	sort(tVector.begin(),tVector.end(),cmp);

	//for test 
#if 0
	cout<<"tvector start Orderly output "<<endl;
	for(int i=0;i<tVector.size();i++)  
	{  
		cout<<tVector[i].first<<":"<<tVector[i].second<<endl;  
	} 
	cout<<"tvector end"<<endl;
#endif
}

/*****************************************************************
Function Name: PrintfMap
Description: printf the word in mapword[]
Parameter:  
@filecounts: a Parameter to mean total number of file
Author: SA12226340(Song Zeyu)
Date: 2013-05-13
******************************************************************/
void PrintfMap(int filecounts)
{
	int index;
	map <string,int> ::iterator iter;
	cout<<"mapword order as following"<<endl;
	for(index=0;index<filecounts;index++)
	{
		cout<<"the "<<index<<" map wordmap"<<endl;
		for(iter = mapword[index].begin();iter!=mapword[index].end();iter++)
		{	
			cout<<iter->first<<":"<<iter->second<<endl;
		}
	}
	
}

/*****************************************************************
Function Name: ClearMapWord
Description: clear the mapword[index]
Parameter:  
@index: a Parameter to mean to clear the mapword[index]
Author: SA12226340(Song Zeyu)
Date: 2013-05-13
******************************************************************/
void ClearMapWord(int index)
{
	mapword[index].clear();
}

/*****************************************************************
Function Name: StatisticsMapWords
Description: statistics the word numbers of all map and insert the word in spam_map<string,int>
Parameter:  
@N: a Parameter to mean the numbers need to statistics mapword
Author: SA12226340(Song Zeyu)
Date: 2013-05-13
******************************************************************/
void StatisticsMapWords(int N)
{
	int index;
	map <string,int>::iterator iter;
	map <string,int>::iterator iter2;
	for(index=0;index<N;index++)
	{
		for(iter = mapword[index].begin();iter!=mapword[index].end();iter++)
		{
			iter2 = spam_map.find(iter->first);
			if(iter2 != spam_map.end())//if the iter ->first exists in spam_map,then increase the counts
			{
				++(iter2->second);
			}
			else//if the iter->first does exirst in spam_map,then insert the spam_map 
			{
				spam_map.insert(pair_map(iter->first,1));
			}
		}
	
	}
}

/*****************************************************************
Function Name: PrintfSpam_map
Description: printf the word and the word number of spam_map
Parameter:  

Author: SA12226340(Song Zeyu)
Date: 2013-05-13
******************************************************************/
void PrintfSpam_map()
{
	int index;
	map <string,int> ::iterator iter;
	cout<<"spam_map order as following"<<endl;
	for(iter = spam_map.begin();iter!=spam_map.end();iter++)
	{	
		cout<<iter->first<<":"<<iter->second<<endl;
	}
}
#endif
